﻿Public Class frmSocio
    Dim consulta As New consultas
    Dim validar As New Validations
    Dim id_dire As String
    Dim id_club As String

    Private Sub frmSocio_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: esta línea de código carga datos en la tabla 'TPI_PAVDataSet.club' Puede moverla o quitarla según sea necesario.
        Me.ClubTableAdapter.Fill(Me.TPI_PAVDataSet.club)
        Me.txtNroSocio.Text = ""
        habilitaLabels(False)
        habilitaBotones(True)
    End Sub

    Private Sub btnSalir_Click(sender As System.Object, e As System.EventArgs) Handles btnSalir.Click
        Dim Respuesta As Integer = MessageBox.Show("¿Esta seguro que quiere salir?", "Confirmar", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If Respuesta = 1 Then
            End
        End If
    End Sub

    Private Sub btnAgregarImagen_Click(sender As System.Object, e As System.EventArgs) Handles btnAgregarImagen.Click
        Me.openImage.ShowDialog()
    End Sub

    Private Sub btnNuevo_Click(sender As System.Object, e As System.EventArgs) Handles btnNuevo.Click
        Me.txtNroSocio.Text = ""
        habilitaLabels(True)
        habilitaBotones(False)
        Me.btnEliminar.Enabled = False
    End Sub
    Private Sub btnBuscar_Click(sender As System.Object, e As System.EventArgs) Handles btnBuscar.Click
        SocioBuscar.dgvSocios.DataSource = consulta.Consultar("socio")
        SocioBuscar.Show()
    End Sub

    Private Sub btnGuardar_Click(sender As System.Object, e As System.EventArgs) Handles btnGuardar.Click
        Dim valida As New Validations
        Dim domicilio As New Domicilio
        Dim socio As New Socio



        If (valida.validarNoNull(Me)) Then
            If (valida.validaComboBox(Me)) Then
                If (valida.validaDatePickers(Me)) Then
                    If (Me.txtNroSocio.Text = "") Then
                        Try

                            domicilio.id_direccionInfo = Me.id_dire
                            domicilio.calleInfo = Me.txtCalle.Text
                            domicilio.numeroInfo = Me.txtNro.Text
                            domicilio.codigoPostalInfo = Me.txtCodPostal.Text
                            domicilio.deptoInfo = Me.txtDpto.Text
                            domicilio.pisoInfo = Me.txtPiso.Text

                            socio.apellidoInfo = Me.txtApellido.Text
                            socio.nombreInfo = Me.txtNombre.Text
                            socio.nroDocInfo = Me.txtNroDoc.Text
                            socio.tipoDocInfo = Me.cmbTipoDoc.Text
                            socio.fechaNacInfo = Me.dtpFechaNac.Text
                            socio.domicilioInfo = domicilio
                            socio.clubInfo.idInfo = Me.cmbClub.SelectedValue
                            If (Me.ckbEsJugador.Checked = True) Then
                                socio.esJugadorInfo = True
                            Else
                                socio.esJugadorInfo = False
                            End If
                            socio.fechaAltaInfo = Me.dtpFechaAlta.Text

                            MsgBox(socio.domicilioInfo.id_direccionInfo)
                            If (Me.txtNroSocio.Text = "") Then
                                consulta.InsertSocio(socio)

                            Else
                                socio.nroSocioInfo = Me.txtNroSocio.Text
                                consulta.ModificarSocio(socio)
                            End If
                        Catch
                        End Try

                        habilitaBotones(True)
                        habilitaLabels(False)
                    End If
                End If
            End If
        End If

    End Sub

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        If (Me.txtNroSocio.Text = "") Then
            MessageBox.Show("Seleccione el item a eliminar", "Atencion", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        Else
            If (MessageBox.Show("¿Esta seguro que desea eliminar el socio?", "Advertencia", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) = 6) Then
                consulta.Eliminar("socio", Me.txtNroSocio.Text)
                habilitaBotones(True)
                habilitaLabels(False)
                SocioBuscar.dgvSocios.DataSource = consulta.Consultar("socio")
            End If
        End If
    End Sub


    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        validar.resetCheckBox(Me)
        validar.resetComboBox(Me)
        validar.resetRdButton(Me)
        validar.resetTextBox(Me)
        habilitaBotones(True)
        habilitaLabels(False)
    End Sub

    Public Sub habilitaBotones(ByVal param As Boolean)
        Me.btnBuscar.Enabled = param
        Me.btnSalir.Enabled = param
        Me.btnNuevo.Enabled = param
        Me.btnGuardar.Enabled = Not (param)
        Me.btnCancelar.Enabled = Not (param)
        Me.btnEliminar.Enabled = Not (param)
        Me.btnAgregarImagen.Visible = Not (param)
    End Sub

    Public Sub habilitaTextbox(ByVal param As Boolean)

        Me.txtApellido.Enabled = param
        Me.txtNombre.Enabled = param
        Me.txtNroDoc.Enabled = param
        Me.cmbTipoDoc.Enabled = param
        Me.dtpFechaNac.Enabled = param
        Me.txtCalle.Enabled = param
        Me.txtNro.Enabled = param
        Me.txtPiso.Enabled = param
        Me.txtDpto.Enabled = param
        Me.txtCodPostal.Enabled = param
        Me.cmbClub.Enabled = param
        Me.ckbEsJugador.Enabled = param
        Me.dtpFechaAlta.Enabled = param

    End Sub

    Public Sub habilitaLabels(ByVal param As Boolean)
        Me.habilitaTextbox(param)
        Me.txtNroSocio.Text = ""
        Me.txtApellido.Text = ""
        Me.txtNombre.Text = ""
        Me.txtNroDoc.Text = ""
        Me.cmbTipoDoc.Text = ""
        Me.dtpFechaNac.Text = ""
        Me.txtCalle.Text = ""
        Me.txtNro.Text = ""
        Me.txtPiso.Text = ""
        Me.txtDpto.Text = ""
        Me.txtCodPostal.Text = ""
        Me.cmbClub.Text = ""
        Me.ckbEsJugador.Checked = False
        Me.dtpFechaAlta.Text = ""
    End Sub

    'Getters & Setters
    Public Property id_direInfo() As Integer
        Get
            Return id_dire
        End Get
        Set(ByVal value As Integer)
            Me.id_dire = value
        End Set
    End Property

    Public Property id_clubInfo() As Integer
        Get
            Return id_club
        End Get
        Set(value As Integer)
            Me.id_club = value
        End Set
    End Property

End Class